package com.gaojinqi.sys.modular.rbac.service.impl;

import com.gaojinqi.base.common.service.imp.BaseServiceImpl;
import com.gaojinqi.sys.core.shiro.util.PasswordHelper;
import com.gaojinqi.sys.core.shiro.util.UserCacheHelper;
import com.gaojinqi.sys.modular.rbac.dao.UserRepository;
import com.gaojinqi.sys.modular.rbac.dao.UserRoleRepository;
import com.gaojinqi.sys.modular.rbac.model.User;
import com.gaojinqi.sys.modular.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author gaojinqi
 * @version 1.0
 * @since 2020年04月21日
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User, String> implements UserService {

    @Autowired
    private UserRoleRepository userRoleRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserCacheHelper userCacheHelper;

    @Override
    public void save(User entity) {
        if (entity.getPassword() != null) {
            if (entity.getSalt() == null) {
                entity.setSalt(PasswordHelper.generateSalt());
            }
            entity.setPassword(PasswordHelper.encryptPassword(entity.getPassword(), entity.getSalt()));
        }
        super.save(entity);
    }

    @Override
    public void update(User entity) {
        if (entity.getPassword() != null) {
            entity.setPassword(PasswordHelper.encryptPassword(entity.getPassword(), entity.getSalt()));
        }
        super.update(entity);
        userCacheHelper.deleteByUserId(entity.getUserId());
    }

    @Override
    public void deleteById(String userId) {
        // 删除角色用户关系
        userRoleRepository.deleteByUserId(userId);
        // 删除用户
        super.deleteById(userId);
        userCacheHelper.deleteByUserId(userId);
    }

    @Override
    public void deleteByIds(List<String> userIds) {
        // 删除角色用户关系
        userRoleRepository.deleteByUserIds(userIds);
        // 删除用户
        super.deleteByIds(userIds);
        userIds.forEach(userId -> userCacheHelper.deleteByUserId(userId));
    }

    @Override
    public User findById(String id) {
        User userInCache = userCacheHelper.getById(id, User.class);
        if (userInCache != null) {
            return userInCache;
        }
        User user = super.findById(id);
        userCacheHelper.cache(user);
        return user;
    }

    @Override
    public User findByUserName(String userName) {
        User userInCache = userCacheHelper.getByName(userName, User.class);
        if (userInCache != null) {
            return userInCache;
        }
        User user = userRepository.findByUserName(userName);
        userCacheHelper.cache(user);
        return user;
    }

    @Override
    public User findByPhone(String phone) {
        User userInCache = userCacheHelper.getByPhone(phone, User.class);
        if (userInCache != null) {
            return userInCache;
        }
        User user = userRepository.findByPhone(phone);
        userCacheHelper.cache(user);
        return user;
    }

}
